<?php
	namespace App\Http\Goods;  
	use Illuminate\Database\Eloquent\Model;  
	use Illuminate\Support\Facades\DB;
	use \Illuminate\Http\Request;
	use Session; 
	class GoodsModel extends Model{
			/**
		 * [classify 分类列表]
		 * @return [arr] [数据]
		 */
		public function cate($type = 1){
			$arr = DB::table("mo_cate")->get();
			$data = $this->wxj($arr , 0 , 0 , $type);
			return $data;
		}


		/**
		 * [wxj 无限极递归分类]
		 * @param  [arr]  $arr    [全部数据]
		 * @param  integer $id    [分类的classify_id]
		 * @param  integer $level [有层次感]
		 * @return [arr]          [递归整理好的数据]
		 */
		public function wxj($arr , $id = 0 , $level = 0 , $type = 1){
			static $data;
			static $da;

			foreach($arr as $k => $v){
				if($v->parent_id == $id){
					$v->cate_name =  str_repeat('&nbsp;&nbsp;', $level)."└―".$v->cate_name;
					$data[]  = $v;
					
					$this->wxj($arr , $v->cate_id , $level + 2);
				}$datas[] = $data;
			}
			if($type == 1){
				return $data;
			}else{
				return $datas;
			}
			
		}
		//查询顶级分类

		public function parent_a($id){
			$data = DB::table('mo_cate')->where('cate_id','=',$id)->first();
			if($data->parent_id != 0){ 
				return $this->parent_a($data->parent_id);
			}
			return $data->cate_id;
		}
	/*	//查询属性（颜色、尺码、大小、规格）
		public function type_a($id){
			$data = DB::table('mo_cate')->where('cate_id','=',$id)->get();
			return $data;
		}
		//查询商品的顶级分类
		public function sess(){
			$sess = Session::get('g_name');
			if($sess == ""){
				return null;
			}else{
				$data = DB::table('mo_goods')->where('g_name','=',$sess)->first();
				//取出分类id	
				$cate = $data->g_cate_id;
				return $cate;
			}
		}*/
		//查询商品的顶级分类
		public function goods_attr($id){
			//判断id是否等于空如果等于空session里取出name值查询分类id 否则就根据传递过来的id查询分类id
			$data = DB::table('mo_goods')->where('g_id','=',$id)->first();
			//取出分类id	
			$cate_id = $data->g_cate_id;
			$attr = $this->parent_a($cate_id);
			return $attr;
		}
		//查询商品分类的属性名
		public function attr_name($id){
			$data = DB::table('mo_type_name')->where('cate_id','=',$id)->get();
			foreach($data as $v){
				$value_id[] = $v->id;
			}
			return $value_id;
		}
		//查询商品属性值
		public function attr_value($id){
			foreach($id as $v){
				$data[] = DB::table('mo_type_value')->where('attrname_id','=',$v)->get();
			}
			return $data;
		}
		//查询所有的顶级分类
		public function parent(){
			$data = DB::table('mo_cate')->where('parent_id','0')->get();
			return $data;
		}
		//根据传递过来的商品分类id来查询商品的属性
		public function attrName($id){
			$data = DB::table('mo_type_name')->where('cate_id','=',$id)->get();
			return $data;
		}
		 public function getData($table,$where){
                        $data = DB::table($table)->where($where)->get();
                        return $data;
                }
	          public function selectData($table){
                        $data = DB::table($table)->get();
                        return $data;
                }
			 public function selectedData($table,$where,$name,$sort,$page){
			       return DB::table($table)->whereraw($where)->orderBy($name, $sort)->Paginate($page);
			 
			    }
		

	}
?>
